package com.ml.mall.service.log.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.bean.req.log.ExceptionLogQueryDto;
import com.ml.mall.entity.log.ExceptionLog;
import com.ml.mall.mapper.log.ExceptionLogMapper;
import com.ml.mall.mapper.sys.SysUserMapper;
import com.ml.mall.service.log.ExceptionLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kyyang
 * Created 2021-01-07
 */
@Service
public class ExceptionLogServiceImpl extends ServiceImpl<ExceptionLogMapper, ExceptionLog> implements ExceptionLogService {

    @Autowired
    private ExceptionLogMapper exceptionLogMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public IPage<ExceptionLog> exceptionQuery(ExceptionLogQueryDto exceptionLogQueryDto) {
        IPage<ExceptionLog> page = new Page<>(exceptionLogQueryDto.getPageIndex(),exceptionLogQueryDto.getPageNum());
        QueryWrapper<ExceptionLog> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(exceptionLogQueryDto.getExcIp())){
            wrapper.eq("exc_ip",exceptionLogQueryDto.getExcIp());
        }
        if(StringUtils.isNotBlank(exceptionLogQueryDto.getExcUserId())){
            wrapper.eq("exc_user_id",exceptionLogQueryDto.getExcUserId());
        }
        if(StringUtils.isNotBlank(exceptionLogQueryDto.getExcVer())){
            wrapper.eq("exc_ver",exceptionLogQueryDto.getExcVer());
        }
        if(StringUtils.isNotBlank(exceptionLogQueryDto.getExcUserName())){
            wrapper.like("exc_user_name",exceptionLogQueryDto.getExcUserName());
        }
        if(exceptionLogQueryDto.getStartTime()!=null){
            wrapper.ge("exc_create_time",exceptionLogQueryDto.getStartTime());
        }
        if(exceptionLogQueryDto.getEndTime()!=null){
            wrapper.le("exc_create_time",exceptionLogQueryDto.getEndTime());
        }

        wrapper.orderByDesc("exc_create_time");
        IPage<ExceptionLog> exceptionLogIPage = exceptionLogMapper.selectPage(page, wrapper);

        return exceptionLogIPage;
    }
}
